/**
 * 用户实体
 * 作者：GodMainCode
 * 创建时间：2024-01-17
 * 修改时间：2024-01-17
 * 修改人：GodMainCode
 */

import { 
  Entity, 
  Column, 
  PrimaryGeneratedColumn, 
  CreateDateColumn, 
  UpdateDateColumn, 
  Index,
  ManyToMany,
  JoinTable
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { Role } from '../../role/entities/role.entity';

@Entity('user')
export class User {
  @ApiProperty({ description: '用户ID' })
  @PrimaryGeneratedColumn({ comment: '用户ID' })
  id: number;

  @ApiProperty({ description: '用户名' })
  @Index('idx_username')
  @Column({ length: 50, comment: '用户名' })
  username: string;

  @ApiProperty({ description: '密码' })
  @Column({ length: 100, comment: '密码' })
  password: string;

  @ApiProperty({ description: '邮箱' })
  @Index('idx_email')
  @Column({ length: 100, nullable: true, comment: '邮箱' })
  email: string;

  @ApiProperty({ description: '状态：0-禁用 1-启用' })
  @Column({ 
    type: 'smallint',
    default: 1,
    comment: '状态：0-禁用 1-启用'
  })
  status: number;

  @ApiProperty({ description: '创建时间' })
  @CreateDateColumn({ 
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '创建时间'
  })
  created_at: Date;

  @ApiProperty({ description: '更新时间' })
  @UpdateDateColumn({ 
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '更新时间'
  })
  updated_at: Date;

  @ApiProperty({ description: '是否删除：0-否 1-是' })
  @Column({ 
    type: 'smallint',
    default: 0,
    comment: '是否删除：0-否 1-是'
  })
  is_deleted: number;

  @ApiProperty({ description: '用户角色', type: () => [Role] })
  @ManyToMany(() => Role, role => role.users)
  @JoinTable({
    name: 'user_role',
    joinColumn: { name: 'user_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'role_id', referencedColumnName: 'id' },
  })
  roles: Role[];
} 